Preparing the data

### Notes
# install.packages('ggplot2',dependencies = T)
# Test code line with command+enter
# update.packages(checkBuilt = T,ask = F)
# head(ds) # first few lines of a dataframe
# str(ds) #show structure of dataframe
###

# Setting variables:
options(width=108)
outT <- 2.5 # Outlier factor

# Loading libraries
library(ggplot2) # for plotting
library(plyr)

#Importing the data
#data_dir <- '/Volumes/PK/'
#homeDir <- path.expand('~/..')
homeDir <- path.expand('~')
dataDir <- paste(homeDir, 'Dropbox/Projects/fc/fc/data/usable/extracted', sep='/')
dataFiles <- dir(dataDir, pattern='csv')
nSubj <- length(dataFiles) # number of subjects

# Going through all subjects' data:
df <- data.frame() # data frame that combines all subjects' data into a single ds
#curSubjN <- 6 #temp

for(curSubjN in 1:nSubj){
    # Loading the data;
    ds <- read.csv(paste(dataDir, dataFiles[curSubjN], sep='/'))
    # Shorter names for variables:
    colnames(ds) <- c('subjId', 'domEyeR', 'threshStHi', 'threshStLo', 'thresh', 'trialN', 'sentId',
                      'sentPx', 'congr', 'fam', 'locTop', 'cued', 'corr', 'broken', 'st')
    # Non-blank data set:
    cds <- ds[ds$sentId<31,]
    # Removing the outliers:
    outlLo <- cds$st>(mean(cds$st)-outT*sd(cds$st)) # binary for every row
    outlHi <- cds$st<(mean(cds$st)+outT*sd(cds$st))
    cds$outlNlo <- sum(!outlLo) # counting the number of outliers before removing
    cds$outlNhi <- sum(!outlHi)
    cds <- cds[outlLo,] # removing the outliers
    cds <- cds[outlHi,]
    cds <- cds[!is.na(cds$subjId),] # usually don't need this; remove NA rows
    # Normalized suppression times:
    cds$stNorm <- cds$st / mean(cds$st)    
    # Binding the subject ds to the common data frame:
    df <- rbind(df, cds)
}
# Prettying up the data frame:
df$subjId <- factor(df$subjId)
df$Familiarity[df$fam==1] <- 'Familiar\n(Upright)'
df$Familiarity[df$fam==0] <- 'Unfamiliar\n(Inverted)'
df$Congruency[df$congr==1] <- 'Congruent'
df$Congruency[df$congr==0] <- 'Incongruent'
df$Attention[df$cued==1] <- 'Cued'
df$Attention[df$cued==0] <- 'Uncued'
# Binning
df$bin <- 6
df$bin[df$trialN<500] <- 5
df$bin[df$trialN<400] <- 4
df$bin[df$trialN<300] <- 3
df$bin[df$trialN<200] <- 2
df$bin[df$trialN<100] <- 1
head(df)
##   subjId domEyeR threshStHi threshStLo  thresh trialN sentId sentPx congr fam locTop cued corr broken
## 1     14       1    0.31181    0.23408 0.27294      1      5    133     1   0      1    1    1      1
## 2     14       1    0.31181    0.23408 0.27294      2     28    108     1   0      0    0    1      1
## 3     14       1    0.31181    0.23408 0.27294      5     27    104     0   0      1    1    1      1
## 4     14       1    0.31181    0.23408 0.27294      6     21    109     0   0      1    1    1      1
## 5     14       1    0.31181    0.23408 0.27294      8     18    117     0   0      1    1    1      1
## 6     14       1    0.31181    0.23408 0.27294      9     10    128     0   0      0    0    1      1
##       st outlNlo outlNhi    stNorm            Familiarity  Congruency Attention bin
## 1 1.4901       0       7 1.0650477 Unfamiliar\n(Inverted)   Congruent      Cued   1
## 2 1.2797       0       7 0.9146645 Unfamiliar\n(Inverted)   Congruent    Uncued   1
## 3 1.5523       0       7 1.1095051 Unfamiliar\n(Inverted) Incongruent      Cued   1
## 4 1.4809       0       7 1.0584720 Unfamiliar\n(Inverted) Incongruent      Cued   1
## 5 1.3432       0       7 0.9600510 Unfamiliar\n(Inverted) Incongruent      Cued   1
## 6 1.1362       0       7 0.8120980 Unfamiliar\n(Inverted) Incongruent    Uncued   1

Quality control

Summary stats per participant

ddply(df, c('subjId'), summarise, outlNumLow = median(outlNlo), outlNumHigh = median(outlNhi),
      cuedCaught = sum(cued & corr), uncuedCaught = sum(!cued & corr))
##    subjId outlNumLow outlNumHigh cuedCaught uncuedCaught
## 1      14          0           7        116          116
## 2      17          0           9        115          116
## 3      19          0          10        113          107
## 4      21          0           6        118          116
## 5      23          0           6        117          116
## 6      24          0           0        105          101
## 7      25          0           9        115          116
## 8      26          0           9        118          113
## 9      27          0          11        110          115
## 10     28          1           7        115          116
## 11     29          0          14        114          112
## 12     30          0           5        118          117
## 13     31          0           7        116          115
## 14     32          0           7        119          114
## 15     33          0          11        107          104
## 16     34          0           9        113          117
## 17     36          0           6        118          115
## 18     37          0           4        119          117
## 19     38          1           4        115          116
## 20     39          0           7        117          116
## 21     41          0           7        119          114
## 22     42          0           6        120          114
## 23     43          4           4        115          116
## 24     44          0           6        118          111

sts across trials

for(curSubjN in 1:nSubj){
    curSubjId <- levels(df$subjId)[curSubjN]
    print(curSubjId)
    ss <- df[df$subjId==curSubjId,]
    p <- ggplot(data=ss, aes(x=trialN, y=st)) +  geom_line() + theme_bw() + ylim(.5,2) +
        labs(x='Trial Number', y='Suppression Time', title=paste('Participant', as.character(curSubjId))) + 
        theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
              axis.text=element_text(size=8), axis.title=element_text(size=9),
              legend.text=element_text(size=8), legend.title=element_text(size=9),
              legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
              legend.background = element_rect(fill='transparent'))
    plot(p)
}
## [1] "14"

## [1] "17"

## [1] "19"

## [1] "21"
## Warning: Removed 1 rows containing missing values (geom_path).

## [1] "23"
## Warning: Removed 1 rows containing missing values (geom_path).

## [1] "24"

## [1] "25"

## [1] "26"

## [1] "27"

## [1] "28"

## [1] "29"
## Warning: Removed 1 rows containing missing values (geom_path).

## [1] "30"

## [1] "31"

## [1] "32"

## [1] "33"

## [1] "34"

## [1] "36"

## [1] "37"

## [1] "38"

## [1] "39"
## Warning: Removed 2 rows containing missing values (geom_path).

## [1] "41"
## Warning: Removed 2 rows containing missing values (geom_path).

## [1] "42"

## [1] "43"
## Warning: Removed 1 rows containing missing values (geom_path).

## [1] "44"

Individual plots

Congruence X Familiarity X Cue

for(curSubjN in 1:nSubj){
    curSubjId <- levels(df$subjId)[curSubjN]
    ss <- df[df$subjId==curSubjId,]
    p <- ggplot(data=ss, aes(x=factor(Familiarity), y=st, colour=factor(Congruency))) +  geom_boxplot() + 
        facet_grid(~Attention) + theme_bw() +
        labs(x='Familiarity', y='Suppression Time', colour='Congruency',
             title=paste('Participant', as.character(curSubjId))) + 
        theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
              axis.text=element_text(size=8), axis.title=element_text(size=9),
              legend.text=element_text(size=8), legend.title=element_text(size=9),
              legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
              legend.background = element_rect(fill='transparent'))
    plot(p)
}

Group plots

Is cueing effective across participants?

p <- ggplot(data=df, aes(x=Attention, y=st)) + geom_boxplot() + facet_grid(~subjId) + theme_bw() +
    labs(x='Attention effect X Subject', y='Suppression Time', colour='Attention',
         title='Effect of attentional cueing') +
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(.0, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

Is cueing effective across trials?

p <- ggplot(data=df, aes(x=Attention, y=st)) + geom_boxplot() + facet_grid(~bin) + theme_bw() +
    labs(x='Attention effect X 100-trial bins', y='Suppression Time', colour='Attention',
         title='Effect of attentional cueing') +
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(.0, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

Congruence X Familiarity X Cue

dfSum <- ddply(df, .(subjId, Attention, Familiarity, Congruency), summarise, 
               `Mean Normalized ST` = mean(stNorm, na.rm=T))
p <- ggplot(data=dfSum, aes(x=factor(Familiarity), y=`Mean Normalized ST`, 
                            colour=factor(Congruency))) + 
    geom_boxplot() +  facet_grid(~Attention) + theme_bw() +
    labs(x='Familiarity', y='Suppression Time', colour='Congruency',
         title='Congruence X Familiarty X Cue') + 
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

p <- ggplot(data=dfSum, aes(x=factor(Familiarity), y=`Mean Normalized ST`, 
                            group=factor(subjId), colour=factor(subjId))) + 
    geom_line() + facet_grid(~Attention*Congruency) + theme_bw() +
    labs(x='Familiarity', y='Suppression Time', colour='Congruency',
         title='Congruence X Familiarty X Cue') + 
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

Linear models

Centered data set

df$congrC <- -1
df$congrC[df$congr==1] <- 1
df$famC <- -1
df$famC[df$fam==1] <- 1
df$cuedC <- -1
df$cuedC[df$cued==1] <- 1
df$topSubj <- 0
df$topSubj[df$locTop==df$cued] <- 1

Mixed linear regressions

#mFull <- lmer(st ~ famC * cuedC * congrC + (1|subjId), df)
library(BayesFactor)
## Loading required package: coda
## Loading required package: Matrix
## ************
## Welcome to BayesFactor 0.9.12-2. If you have questions, please contact Richard Morey (richarddmorey@gmail.com).
## 
## Type BFManual() to open the manual.
## ************
df$subjId <- as.factor(df$subjId)
df$fam<- as.factor(df$fam)
df$cued<- as.factor(df$cued)
df$congr<- as.factor(df$congr)
bf <- anovaBF(st ~ fam * cued * congr + subjId, data=df, whichRandom = 'subjId')
bf
## Bayes factor analysis
## --------------
## [1] congr + subjId                                                                    : 0.03315989   <U+00B1>1.1%
## [2] fam + subjId                                                                      : 0.03010921   <U+00B1>1.38%
## [3] congr + fam + subjId                                                              : 0.001010881  <U+00B1>5.28%
## [4] congr + fam + congr:fam + subjId                                                  : 4.809882e-05 <U+00B1>2.07%
## [5] cued + subjId                                                                     : 3420866      <U+00B1>1.13%
## [6] congr + cued + subjId                                                             : 111867.5     <U+00B1>1.5%
## [7] fam + cued + subjId                                                               : 112615.9     <U+00B1>8.59%
## [8] congr + fam + cued + subjId                                                       : 3469.594     <U+00B1>3.03%
## [9] congr + fam + congr:fam + cued + subjId                                           : 283.9225     <U+00B1>42.6%
## [10] congr + cued + congr:cued + subjId                                               : 13291.54     <U+00B1>3.91%
## [11] congr + fam + cued + congr:cued + subjId                                         : 363.0126     <U+00B1>2.27%
## [12] congr + fam + congr:fam + cued + congr:cued + subjId                             : 18.15136     <U+00B1>2.93%
## [13] fam + cued + fam:cued + subjId                                                   : 5456.2       <U+00B1>17.67%
## [14] congr + fam + cued + fam:cued + subjId                                           : 163.059      <U+00B1>14.97%
## [15] congr + fam + congr:fam + cued + fam:cued + subjId                               : 7.474029     <U+00B1>10.06%
## [16] congr + fam + cued + congr:cued + fam:cued + subjId                              : 16.63911     <U+00B1>5.61%
## [17] congr + fam + congr:fam + cued + congr:cued + fam:cued + subjId                  : 0.8475175    <U+00B1>5.38%
## [18] congr + fam + congr:fam + cued + congr:cued + fam:cued + congr:fam:cued + subjId : 0.08806365   <U+00B1>11.1%
## 
## Against denominator:
##   st ~ subjId 
## ---
## Bayes factor type: BFlinearModel, JZS